package su.kozhevnikov.nookbrary.items;

import java.util.Comparator;

import su.kozhevnikov.nookbrary.interfaces.IItem;

public class ItemComparator
{
	public static class Default implements  Comparator<Object> {
		public int compare(Object o1, Object o2) 
		{
			IItem i1 = (IItem) o1;
			IItem i2 = (IItem) o2;
			
			if (i1.getType().getWeight() != i2.getType().getWeight())
				return (i1.getType().getWeight() > i2.getType().getWeight()) ? 1 : -1;
				
			int result;

			result = i1.getAuthor().compareToIgnoreCase(i2.getAuthor());
			if (result!=0) return result;		

			/*String author1 = i1.getAuthor();
			if (author1.contains("("))
				author1 = author1.substring(0, author1.indexOf("(")).trim();
			author1 = author1.substring(author1.lastIndexOf(" ") + 1);
			String author2 = i2.getAuthor();
			if (author2.contains("("))
				author2 = author2.substring(0, author2.indexOf("(")).trim();
			author2 = author2.substring(author2.lastIndexOf(" ") + 1);
			result = author1.compareToIgnoreCase(author2);
			if (result!=0) return result;	*/	
	
			result =  i1.getSeriesName().compareToIgnoreCase(i2.getSeriesName());
			if (result!=0) return result;
	
			if (i1.getSeriesIndex() != i2.getSeriesIndex())
				return (i1.getSeriesIndex() > i2.getSeriesIndex()) ? 1 : -1;
			
			result =  i1.getTitle().compareToIgnoreCase(i2.getTitle());
			if (result!=0) return result;
			
			
	
			return 0;
		}
	}

	public static class InSeriesMode implements  Comparator<Object> {
		public int compare(Object o1, Object o2) 
		{
			IItem i1 = (IItem) o1;
			IItem i2 = (IItem) o2;
			
			if (i1.getType().getWeight() != i2.getType().getWeight())
				return (i1.getType().getWeight() > i2.getType().getWeight()) ? 1 : -1;
				
			int result;
	
			result =  i1.getSeriesName().compareToIgnoreCase(i2.getSeriesName());
			if (result!=0) return result;
	
			if (i1.getSeriesIndex() != i2.getSeriesIndex())
				return (i1.getSeriesIndex() > i2.getSeriesIndex()) ? 1 : -1;

			result =  i1.getTitle().compareToIgnoreCase(i2.getTitle());
			if (result!=0) return result;
			
			String author1 = i1.getAuthor();
			author1 = author1.substring(author1.lastIndexOf(" ") + 1);
			String author2 = i2.getAuthor();
			author2 = author2.substring(author2.lastIndexOf(" ") + 1);
			result = author1.compareToIgnoreCase(author2);
			if (result!=0) return result;		
	
			return 0;
		}
	}
}
